﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_Flow_DeleteFlow_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_Flow_DeleteFlow_V2];
GO
CREATE PROCEDURE [dbo].[sproc_Flow_DeleteFlow_V2]
    @FlowID int
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;

-- This sp will remove the given flow from the DB.
-- It can be removed only when there is no other flows that have a relationship with it.
BEGIN TRANSACTION;
DECLARE @returnValue int;

SET @returnValue = 0;
IF EXISTS(SELECT 1 FROM dbo.UDS_Flow WHERE Flow_ID = @FlowID)
BEGIN
    IF NOT EXISTS(SELECT 1 FROM dbo.UDS_Flow_Document WHERE Flow_id = @flowid)
    BEGIN 
        --删除绑定
        DELETE FROM UDS_Flow_Member_Bind WHERE Flow_id = @flowid

        --删除流程步骤
        DELETE FROM UDS_Flow_Jump WHERE Flow_ID = @FlowID
        DELETE FROM UDS_Flow_Step WHERE flow_id =@FlowID

        --删除实体
        DELETE FROM UDS_Flow WHERE Flow_id = @FlowID
    
        SET @returnValue = 0;
    END
    ELSE
    BEGIN
        SET @returnValue = -1;
    END
END
ELSE
BEGIN
    SET @returnValue = -2;
END

-- Commit/rollback the transaction
IF ( @returnValue = 0 )
    COMMIT TRANSACTION;
ELSE
    ROLLBACK TRANSACTION;

RETURN @returnValue;

END
GO
